<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

/**
 * Uri class
 */
class Uri {
	/**
	 * Get segment
	 */
	public static function getSegment($num, $result = false) {
		$route = Router::getRoute();
		if (isset($route[$num])) {
			$result = $route[$num];
		}

		return $result;
	}

	/**
	 * Get variable
	 */
	public static function getVariable($variable, $result = false) {
		$routes = Router::getRoute();
		$found = false;
		foreach ($routes as $route) {
			if ($found) {
				$result = $route;
				break;
			} else if (Router::fixRoute($variable) == $route) {
				$found = true;
			}
		}

		return $result;
	}

	/**
	 * Get id
	 */
	public static function getId($result = false) {
		$routes = Router::getRoute();
		foreach ($routes as $route) {
			$subject = $route;
			$pattern = '/(.+)\.' . Config::get('extension', 'uri', 'html') . '$/i';
			if (preg_match($pattern, $subject, $matches)) {
				$result = $matches[1];
				break;
			}
		}

		return $result;
	}

	/**
	 * Get path
	 */
	public static function getPath() {
		$result = array();
		$routes = Router::getRoute();
		foreach ($routes as $route) {
			$subject = $route;
			$pattern = '/(.+)\.' . Config::get('extension', 'uri', 'html') . '$/i';
			if (!preg_match($pattern, $subject)) {
				$result[] = $route;
			}
		}

		return $result;
	}
	
	/**
	 * Get url
	 */
	public static function get() {
		$result = '';
		$routes = Router::getRoute();
		foreach ($routes as $route) {
			if (!empty($result)) {
				$result .= '/';
			}
			$result .= $route;
		}

		return $result;
	}
}
